<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <el-button type="primary" @click="handleAdd">增&emsp; 加</el-button>
        <el-button type="danger" @click="handleDeleteAll">一键删除</el-button>
      </div>
      <div>
        <el-table
          :data="menuList"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="分类名称" prop="title"></el-table-column>
          <el-table-column label="图标" prop="icon">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img
                  :src="scope.row.icon"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.icon"
                  alt=""
                  style="width: 50px"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="是否显示" prop="is_show">
            <template slot-scope="{ row }">
              <el-tag v-if="row.is_show === 1" type="success">显示</el-tag>
              <el-tag v-else type="info">隐藏</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort"></el-table-column>

          <el-table-column label="操作" prop="date" width="270">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                plain
                size="small"
                type="primary"
                @click="handleEdit(scope.row)"
              >编辑
              </el-button
              >
              <el-button
                v-if="scope.row.is_show === 1"
                plain
                size="small"
                type="info"
                @click="handleToggle(scope.row)"
              >
                隐藏
              </el-button>
              <el-button
                v-else
                plain
                size="small"
                type="success"
                @click="handleToggle(scope.row)"
              >
                显示
              </el-button>
              <el-button
                icon="el-icon-delete"
                plain
                size="small"
                type="danger"
                @click="handleDelete(scope.row)"
              >删除
              </el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""
      ><em style="font-style: normal; color: red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmDel">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        class="demo-ruleForm"
        label-width="200px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input
            v-model="form.cate_name"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleAvatarSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img v-if="form.icon" :src="form.icon" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio v-model="form.is_show" :label="1">显示</el-radio>
          <el-radio v-model="form.is_show" :label="0">隐藏</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="handleConfirm">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { deleteData, getData, postData, putData } from '@/api/personalizedMall/transformers'

export default {
  data() {
    return {

      menuList: [],
      total: 0,
      multipleSelection: [],
      deleteDialogVisible: false,
      del_ids: undefined,
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '新增'
      },
      dialogFormVisible: false,
      form: {
        id: undefined,
        cate_name: undefined,
        icon: undefined,
        is_show: 1,
        sort: undefined
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ],
        icon: [{ required: true, message: '请上传图片', trigger: 'change' }],
        is_show: [
          { required: true, message: '请选择显隐状态', trigger: 'change' }
        ],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.handleGetData()
  },
  methods: {
    // 获取列表数据
    handleGetData() {
      getData().then(res => {
        this.menuList = res.data
      })
    },
    // 添加数据
    handleAdd() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 编辑数据
    handleEdit(row) {
      this.dialogStatus = 'update'
      for (let key in this.form) {
        if (key === 'cate_name') {
          this.form[key] = row['title']
        } else {
          this.form[key] = row[key]
        }
      }
      this.dialogFormVisible = true
    },
    // 确认添加
    handleConfirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.dialogStatus === 'create') {
            postData(this.form).then(res => {
              if (res.code === 200) {
                this.handleGetData()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          } else {
            putData(this.form).then(res => {
              if (res.code === 200) {
                this.handleGetData()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 单个删除
    handleDelete(row) {
      this.del_ids = row.id.toString()
      this.deleteDialogVisible = true
    },
    // 一键删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请勾选要删除的数据')
        return
      } else {
        let arr = []
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })
        this.del_ids = arr.join(',')
      }
      this.deleteDialogVisible = true
    },
    // 确认删除
    handleConfirmDel() {
      deleteData(this.del_ids).then(res => {
        if (res.code === 200) {
          this.handleGetData()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },

    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleAvatarSuccess() {
      this.form.icon = this.ossLastImgUrl
    },
    handleClose() {
      this.$refs.form.resetFields()
      this.form = {
        id: undefined,
        cate_name: undefined,
        icon: undefined,
        sort: undefined,
        is_show: 1
      }
    },
    handleToggle(row) {
      for (let key in this.form) {
        if (key === 'cate_name') {
          this.form[key] = row['title']
        } else if (key === 'is_show') {
          this.form[key] = row.is_show ? 0 : 1
        } else {
          this.form[key] = row[key]
        }
      }
      putData(this.form).then(res => {
        if (res.code === 200) {
          this.handleGetData()
          this.dialogFormVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}
</style>
